<template>
  <div class="login_box">
    <el-form
      ref="ruleFormRef"
      :model="user"
      :rules="rules"
      label-width="120px"
      status-icon
    >
      <el-form-item label="用户名" prop="username">
        <el-input v-model="user.username" />
      </el-form-item>

      <el-form-item label="密码" prop="password">
        <el-input v-model="user.password" show-password />
      </el-form-item>

      <el-form-item>
        <el-button @click="goLogin">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
  
  <script setup>
import { ref } from "vue";
import {useRouter} from "vue-router"
//-----------定义数据---------------
//定义表单数据
let user = ref({
  username: "admin",
  password: "123456",
});

//定义验证规则
let rules = ref({
  username: [
    { required: true, message: "请输入用户名", trigger: "blur" },
    { min: 2, max: 8, message: "字符长度为2 到 8位", trigger: "blur" },
  ],
  password: [
    { required: true, message: "请输入密码", trigger: "blur" },
    { min: 6, max: 18, message: "字符长度为6 到 18位", trigger: "blur" },
  ],
});

//定义ref对象
let ruleFormRef = ref(null);

//定义路由对象
let router = useRouter();



//--------------定义方法------------
function goLogin() {
  ruleFormRef.value.validate((val) => {
    if(!val){
      return; //如果验证没有通过什么都不做
    }

    //获取表单数据，发起ajax请求，实现登录的功能，登录成功跳转
    console.log(user.value);
    router.push("/index/home");
  
  });
}
</script>
  
  <style scoped>
.login_box {
  width: 100vw;
  height: 100vh;
  background: rgba(0, 0, 0, 0.2);
}
.el-form {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-70%, -50%);
}
</style>